import os
import shutil

from tqdm import tqdm

def checkDirForDeepIsExists(dir_name):
    if not os.path.exists(dir_name):  # os模块判断并创建
        deep_dir_name = os.path.dirname(dir_name)
        checkDirForDeepIsExists(deep_dir_name)
        os.mkdir(dir_name)

imagePath="G:\数据集\植保所\分类目标图片\外来入侵害虫\深度学习数据集\imagenet230327"
savePath="G:\数据集\植保所\分类目标图片\外来入侵害虫\深度学习数据集\imagenet230327"

for dirpath, dirnames, filenames in tqdm(os.walk(imagePath)):
    index=0
    for filename in filenames:
        #if("jpg" in filename or "png" in filename or "JPG" in filename):
        newFileName=os.path.dirname(os.path.join(dirpath, filename)).split("\\")[-1]+"_"+str(index)+"."+filename.split(".")[-1]
        index=index+1
        #checkDirForDeepIsExists(dirpath.replace(imagePath,savePath))
        # 复制
        #shutil.copy(os.path.join(dirpath, filename),os.path.join(dirpath.replace(imagePath,savePath), newFileName))
        # 重命名
        os.rename(os.path.join(dirpath, filename),os.path.join(dirpath.replace(imagePath,savePath), newFileName))
        #print(os.path.join(dirpath, filename))